<?php
	@header("Content-type:text/html; charset=utf-8"); // @表示忽略此处可能产生的告警或错误; 编辑器的编码格式也必须设置为utf-8
?>

<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8"> <!编辑器的编码格式也必须设置为utf-8>
	<title>迷你订餐系统</title>
	<script type="text/javascript">
	function todayshow() {
		var objDate = new Date();
		var day = objDate.getDay();
		var arrDayMap = new Array(7);
		arrDayMap[0] = "星期日";
		arrDayMap[1] = "星期一";
		arrDayMap[2] = "星期二";
		arrDayMap[3] = "星期三";
		arrDayMap[4] = "星期四";
		arrDayMap[5] = "星期五";
		arrDayMap[6] = "星期六";
		
		var formmonth = objDate.getMonth() + 1;
		var formtoday = objDate.getDate();
		var formhour = objDate.getHours();
		var formminute = objDate.getMinutes();
		if(formmonth < 10) formmonth = "0" + formmonth;
		if(formtoday < 10) formtoday = "0" + formtoday;
		if(formhour < 10) formhour = "0" + formhour;
		if(formminute < 10) formminute = "0" + formminute;
		var formdate = objDate.getFullYear() + "-" + formmonth + "-" + formtoday;
		var formtime = formhour + ":" + formminute;
		return arrDayMap[day] + "  " + formdate + "  " + formtime;
	}

	function oncount(obj, val) { 
		var objBill = document.getElementById("bill");
		if (obj.checked) {	
			objBill.value = parseInt(objBill.value) + val;
		} else {
			objBill.value = parseInt(objBill.value) - val;	
		}
	}

	function onclear() {
		var objBill = document.getElementById("bill");
		objBill.value = 0;
		var objMenu = document.getElementsByName("menu[]");     
		var len = objMenu.length;     
		for (var i=0; i<len; i++) {                    
			objMenu[i].checked = false;
		}	 
	}

	function validate() {
		var objBill = document.getElementById("bill");
		if (parseInt(objBill.value) == 0) {
			return confirm("对不起，您没有点餐。如已定过餐继续提交会取消您上次的订餐，？");
		}
		
		if ((parseInt(objBill.value) > 0) && (parseInt(objBill.value) < 8)) {
			alert("定餐金额不能少于8元！");
			return false;
		} 
	}

	function getCookie(c_name) {
		if(document.cookie.length > 0) {
			var c_start = document.cookie.indexOf(c_name + "=");
			if(c_start != -1) { 
				c_start = c_start + c_name.length + 1; 
				var c_end = document.cookie.indexOf(";", c_start);
				if(c_end == -1) {
					c_end = document.cookie.length;
				}
				
				return document.cookie.substring(c_start, c_end);
			} 
		}
		
		return "";
	}

	function setName(c_value) {
		var objName = document.getElementById("name"); // 等于document.all.name
		for($i=0; $i<objName.length; $i++) {
			if(objName.options[$i].value == c_value) {
				objName.selectedIndex = $i;
				break;
			}
		}
	}
	</script>
</head>

<body>
<div align="center">
	<table width="692" height="96" border="0" cellspacing="0" cellpadding="0"><tr align="center">
	<td width="30%">
	<a href="../menuqwds/srcmenu.php"><img src="res/t53.png" width="96" height="64" border="0" /></a>
	<img src="res/t0.png" width="96" height="96" />
	</td>
	<td width="40%">
    <img src="res/t9.png" width="192" height="48" />
	<br />
    <span style="color:#ff8800"><b>
    <script type="text/javascript">
    document.write(todayshow()) 
    </script>
	</b></span>
	</td>
	<td width="30%">
	<img src="res/t1.png" width="96" height="96" border="0" />
	<a href="destmenu.php"><img src="res/t35.png" width="96" height="64" border="0" /></a>
	</td>
	</tr></table>
	
	<form method="post" action="destmenu.php" onsubmit="return validate()">
    <img src="res/t32.png" width="692" height="48" />

<?php // <?兼容性不如<?php, <?必须在php.ini中配置short_open_tag=on，否则会导致整个php页面乱码
	// 从配置文件中解析今日菜单
	$arr = file("../menuqwds/srcmenu.ini"); // 返回分行数组
	$totallines = count($arr); // 行数
	$todaymenu = array(array(), array(), array(), array(), array(), array()); // 菜品分一到五元，另有零元为免费例汤或其它赠送
	$istodaymenu = false;
	@$sectionname = "[" . date("l") . "]"; // @表示忽略此处可能产生的告警或错误(为date("l"))
	for($i=2; $i<$totallines; $i++) { // 跳过开始两行的快餐店信息Section(内部的三个逻辑相关的if语句也可改为if...else if...else...形式)
		if(stristr($arr[$i], $sectionname)) {
			$istodaymenu = true;
			continue;
		}
		
		if(stristr($arr[$i], "[") && $istodaymenu) {
			break;
		}
		
		if($istodaymenu) {
			$separator = strpos($arr[$i], "=");
			$name = substr($arr[$i], 0, $separator);
			$price = substr($arr[$i], $separator+1);
			trim($name);
			trim($price);
			$price = intval($price);
			if(($price < 0) || ($price > 5)) { // 本菜单目前只支持一到五元的菜品(以及零元的免费例汤等)，此处旨在屏蔽用户在配置文件中书写错误而导致程序问题
				continue;
			}
			
			$todaymenu[$price][] = $name; // array_push($todaymenu[$price], $name)效率低于此句
		}
	}
	
	// 构造显示今日菜单
	echo "<table width='692' border='0' cellspacing='1' cellpadding='0'>";
	echo "<tr height='24'><td colspan='7' background='res/t2.png' /></tr>";
	echo "<tr align='center'><td width='23' background='res/t4.png' />";
	$count1 = count($todaymenu);
	for($i=$count1-1; $i>0; $i--) { // 分别处理五元到一元(解析数组的5到1索引)的菜单，零元(0索引)通常为免费例汤而放在最后处理
		echo "<td valign='top' bgcolor='yellow'><table width='128'>";
		$count2 = count($todaymenu[$i]);
		for($j=0; $j<$count2; $j++) {
		    $wordcount = strlen($todaymenu[$i][$j]) / 3; // utf-8每个汉字占3个字节，而显示时界面只占两个字符宽度，因此对齐时一个汉字要补多一个字节(如果菜名中意外出现非汉字，则显示宽度会长出一个字符)
			$val = str_pad($todaymenu[$i][$j], 12+$wordcount, "-") . $i . "元"; // 菜名长度暂定为7个汉字内，即界面显示对齐为14个字符宽度(如果意外超出则均会紧挨着价格显示)
			//$sel = '<input type="checkbox" name="menu[]" value="$val" onclick="oncount(this, $i)">'; // 单引号内的双引号内变量在此被当作普通字符，下步无法再解析，因此达不到需求
			$sel = "<input type='checkbox' name='menu[]' value='$val' onclick='oncount(this, $i)'>"; // 双引号内的单引号内变量在此被解析	
			echo "<tr align='left'><td style='color:green'>" . $sel . $todaymenu[$i][$j] . "</td></tr>";
		}
		
		echo "</table></td>";
	}

	echo "<td width='23' background='res/t2.png' /></tr>";
	echo "<tr height='24'><td colspan='7' background='res/t4.png' /></tr>";
	echo "</table><br />";
	$freecount = count($todaymenu[0]);
	for($i=0; $i<$freecount; $i++) {
		echo "<span style='color:green'> Free" . $todaymenu[0][$i] . "</span>";
	}
?>
	
	<br />
	<input type="button" value="清空" onclick="onclear()" />
	<select id="name" name="name">
	<option value="及时雨宋江">及时雨宋江</option>
	<option value="玉麒麟卢俊义">玉麒麟卢俊义</option>
	<option value="智多星吴用">智多星吴用</option>
	<option value="浪子燕青">浪子燕青</option>
	<option value="九纹龙史进">九纹龙史进</option>
	<option value="双鞭呼延灼">双鞭呼延灼</option>
	<option value="大刀关胜">大刀关胜</option>
	<option value="霹雳火秦明">霹雳火秦明</option>
	<option value="豹子头林冲">豹子头林冲</option>
	<option value="青面兽杨志">青面兽杨志</option>
	<option value="小李广花荣">小李广花荣</option>
	<option value="黑旋风李逵">黑旋风李逵</option>
	<option value="行者武松">行者武松</option>
	<option value="花和尚鲁智深">花和尚鲁智深</option>
	</select>
	<input type="text" size="4" readonly="readonly" id="bill" name="bill" value="0" />
	<input type="submit" value="提交" />
	</form>
	<script type="text/javascript">
	// 当保存名字时可使用下述方法，其中文名字兼容性不好
    //setName(getCookie("selectedName"))
	// 当保存索引时则需配合下述方法使用
	document.all.name.selectedIndex = parseInt(getCookie("selectedIndex")) // 等于document.getElementById("name").selectedIndex = parseInt(getCookie("selectedIndex"))
    </script>
</div>
</body>
</html>
